package com.zj.jdbc2ThreadLocal;

import com.zj.bean.UserInfo;
import com.zj.jdbc2ThreadLocal.tools.ServiceInterceptor;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Hello world!
 *
 */
public class AppTest
{
    public static void main( String[] args ) throws SQLException {
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\java_workapace");
        testServiceWithProxy();
    }

    public static void  testService() {
        UserInfo user = new UserInfo();
        user.setName("hanks");
        user.setSex(new Byte("0"));
        user.setPhoneNumber("1654567342");
        user.setAddress("汉中市7号");
        user.setBirthday(new Date());
        JdbcService jdbcService = SingletonEnum.INSTANCE.getJdbcService();
        List<UserInfo> infos = jdbcService.doBusiness(user, 1);
        for (UserInfo uu : infos) {
            System.out.println(uu);
        }
    }

        public static void  testServiceWithProxy() throws SQLException {
            UserInfo user=new UserInfo();
            user.setName("mike3");
            user.setSex(new Byte("1"));
            user.setPhoneNumber("1654567342");
            user.setAddress("汉中市7号");
            user.setBirthday(new Date());
            //创建Enhancer对象，类似于JDK动态代理的Proxy类，下一步就是设置几个参数
            Enhancer enhancer = new Enhancer();
            //设置目标类的字节码文件
            enhancer.setSuperclass(JdbcService2.class);
            //设置回调函数
            enhancer.setCallback(new ServiceInterceptor());
            //这里的creat方法就是正式创建代理类
            JdbcService2 jdbcService = (JdbcService2)enhancer.create();
            List<UserInfo> infos = jdbcService.doBusiness(user, 1);
            for (UserInfo uu:infos) {
                System.out.println(uu);
            }
        }

}
